package com.bpr.webservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpr.webservice.controller.util.Constants;
import com.bpr.webservice.domain.Menu;
import com.bpr.webservice.domain.dto.MenuDto;
import com.bpr.webservice.domain.dto.MenuPageDto;
import com.bpr.webservice.repository.MenuRepository;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired MenuRepository menuRepository;
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseEntity<String> doSave(@RequestBody Menu menu){
		try {
			menuRepository.save(menu);
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.SAVE_SUCCED,
				HttpStatus.OK);
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<String> doDelete(@PathVariable("id") Long id){
		try {
			menuRepository.delete(id);
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.SAVE_SUCCED,
				HttpStatus.OK);
	}
	
	@RequestMapping(value="/findone/{id}", method = RequestMethod.GET)
	public @ResponseBody MenuDto getOne(@PathVariable("id") Long id){
		Menu ma = menuRepository.findOne(id);
		MenuDto dto = new MenuDto();
		if (ma != null){
			dto.constructDto(ma);
		}
		return dto;
	}
	
	@RequestMapping(value = "/search")
	public @ResponseBody Page<MenuDto> findAll(
			@RequestParam("page") Integer page,
			@RequestParam("limit") Integer limit,
			@RequestParam("sort") String sortby,
			@RequestParam("dir") String direction) {
		Page<Menu> searchResult = menuRepository.findAll(new PageRequest(
				page-1, limit, new Sort(Sort.Direction.fromString(direction),sortby)));

		Page<MenuDto> dtoResult = new MenuPageDto(searchResult);
		return dtoResult;
	}
	
	@RequestMapping(value = "/findByNameContains")
	public @ResponseBody Page<MenuDto> findByNameContains(
			@RequestParam("menuname") String name,
			@RequestParam("page") Integer pagenum,
			@RequestParam("limit") Integer limit			
			){
		PageRequest page = new PageRequest(pagenum - 1, limit);
		Page<Menu> searchResult = menuRepository.findMenuByMenuName(name + "%", page);
		Page<MenuDto> dtoResult = new MenuPageDto(searchResult);
		return dtoResult;
	}
	
	@RequestMapping(value = "/form.html")
	public String doForm() {

		return "menu_form";
	}

	@RequestMapping(value = "/list.html")
	public String doList() {
		return "menu_list";
	}
	
}
